<script lang="ts" setup>
import { delRuleAPI, getRuleListAPI } from '@/apis/card';
import type {Rule, RuleListParams } from '@/types/card';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref } from 'vue';
import { utils, writeFileXLSX } from 'xlsx';
// 渲染计费列表
const ruleList = ref<Rule[]>([])
const params = ref<RuleListParams>({
  page: 1,
  pageSize: 10
})
const total = ref(0)

const getRuleList = async () => {
  const res = await getRuleListAPI(params.value)
  ruleList.value = res.data.rows
  total.value = res.data.total
}
getRuleList()
// Map方法映射
const aaaMap= new Map<Rule['chargeType'] ,string>([
  ['duration', '时长收费'],
  ['turn', '按次收费'],
  ['partition', '分段收费']
]) 
// 删除计费数据
const  delCard = async (id: number) => {
  ElMessageBox.confirm('删除计费规则', '确定删除该计费规则吗？', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // 删除
    await delRuleAPI(id)
    getRuleList()
    ElMessage.success('删除成功')
  })
}
// 导出Excel表格
const exoprtExcel=()=>{
  // 创建工作簿（xlse文件）
  const workbook=utils.book_new()
  // 创建工作表格
  const worksheet=utils.json_to_sheet(ruleList.value.map((item,index)=>({
    ...item,
    id:index+1,
    chargeType:aaaMap.get(item.chargeType)
  })))
  // 将excel表格添加到工作簿中
   utils.book_append_sheet(workbook,worksheet,'data1')
  //   添加表头
   utils.sheet_add_aoa(worksheet,[[
'序号','计费规则编号','计费规则名称','免费时长(分钟)','收费上限(元)','计费方式','计费规则',
 ]],{origin:'A1'})

  //  导出下载文件
  writeFileXLSX(workbook,'停车计费规则.xlsx')
}

// 增加计费规则
const dialogVisible=ref(false)
const  addCardRule=()=>{
     dialogVisible.value=true
 
}
</script>

<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="addCardRule">增加停车计费规则</el-button>
      <el-button @click="exoprtExcel">导出Excel</el-button>
      <!-- 添加计费规则 -->
  <el-dialog  v-model="dialogVisible" title="增加计费规则" width="400">
    <el-form label-position="top"  justify="" >
      <el-form-item label="计费规则编号 " >
        <el-input  autocomplete="" style="width: 300px; "/>
      </el-form-item>
      <el-form-item label="计费规则名称 ">
        <el-input  autocomplete=""  style="width: 300px; " />
      </el-form-item>
      <el-form-item >
        <el-select placeholder="计费规则编号">
          <el-option label="Zone No.1" value="shanghai" />
          <el-option label="Zone No.2" value="beijing" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button >取消</el-button>
        <el-button type="primary">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="{row}">
            {{ aaaMap.get(row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{ row }"  >
            <el-button size="small" type="text">编辑</el-button>
            <el-button size="small" type="text" @click="delCard(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.rules .el-form-item {
  padding: 0 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>